using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Tool.BaseClass
{
	public class GClass38
	{
		public static string smethod_0(string string_0, string string_1)
		{
			string text = "";
			System.Net.HttpWebRequest httpWebRequest = null;
			System.Net.HttpWebResponse httpWebResponse = null;
			try
			{
				httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(string_0);
				httpWebRequest.Timeout = 30000;
				httpWebRequest.Method = "HEAD";
				httpWebRequest.CookieContainer = new System.Net.CookieContainer();
				httpWebRequest.Headers.Add("Accept-Language", "zh-CN");
				if (!string.IsNullOrEmpty(string_1))
				{
					httpWebRequest.UserAgent = string_1;
				}
				httpWebRequest.Accept = "text/html, text/css";
				GClass0.smethod_7(httpWebRequest, string_0);
				httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
				if (httpWebResponse.StatusCode != System.Net.HttpStatusCode.OK)
				{
					text = "";
				}
				else
				{
					string input = httpWebResponse.ContentType;
					if (new System.Text.RegularExpressions.Regex("charset", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input).Count > 0)
					{
						input = new System.Text.RegularExpressions.Regex("([ ]*)=([ ]*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Replace(input, "=");
						text = new System.Text.RegularExpressions.Regex("(.*)charset=([^;]*?)").Replace(input, "").Trim();
					}
					GClass38.smethod_2(ref text);
				}
			}
			catch
			{
				text = "";
			}
			finally
			{
				if (httpWebResponse != null)
				{
					httpWebResponse.Close();
				}
				if (httpWebRequest != null)
				{
					httpWebRequest.Abort();
				}
			}
			if (text == "")
			{
				GClass38.smethod_3(ref text, string_0, Encoding.Default, string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.UTF8, string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.ASCII, string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.Unicode, string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.BigEndianUnicode, string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.UTF32, string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.UTF7, string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.GetEncoding("GB2312"), string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.GetEncoding("BIG5"), string_1);
				GClass38.smethod_3(ref text, string_0, Encoding.GetEncoding("GBK"), string_1);
			}
			return text.ToUpper();
		}

		public static Encoding smethod_1(string string_0, string string_1)
		{
			string text = GClass38.smethod_0(string_0, string_1);
			Encoding result;
			if (!string.IsNullOrEmpty(text))
			{
				result = Encoding.GetEncoding(text);
			}
			else
			{
				result = Encoding.UTF8;
			}
			return result;
		}

		public static bool smethod_2(ref string string_0)
		{
			bool result;
			if (string.IsNullOrEmpty(string_0))
			{
				result = false;
			}
			else
			{
				try
				{
					Encoding.GetEncoding(string_0);
					result = true;
				}
				catch
				{
					string_0 = "";
					result = false;
				}
			}
			return result;
		}

		private static void smethod_3(ref string string_0, string string_1, Encoding encoding_0, string string_2)
		{
			if (string_0 == "")
			{
				string input = GClass39.smethod_1(string_1, encoding_0, string_2, null, null);
				if (new System.Text.RegularExpressions.Regex("<meta(.*?)GBK(.*?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input).Count <= 0)
				{
					if (new System.Text.RegularExpressions.Regex("<meta(.*?)UTF-8(.*?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input).Count <= 0)
					{
						if (new System.Text.RegularExpressions.Regex("<meta(.*?)GB2312(.*?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input).Count <= 0)
						{
							if (new System.Text.RegularExpressions.Regex("<meta(.*?)BIG5(.*?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input).Count <= 0)
							{
								if (new System.Text.RegularExpressions.Regex("<meta(.*?)Unicode(.*?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input).Count <= 0)
								{
									if (new System.Text.RegularExpressions.Regex("<meta(.*?)BIG5(.*?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input).Count <= 0)
									{
										if (new System.Text.RegularExpressions.Regex("<meta(.*?)BIG5(.*?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input).Count > 0)
										{
											string_0 = "BIG5";
										}
										else
										{
											System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("<meta(.*?)charset(.*?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
											System.Text.RegularExpressions.MatchCollection matchCollection = regex.Matches(input);
											if (matchCollection.Count > 0)
											{
												string input2 = GClass36.smethod_0(matchCollection[0].Value.Trim());
												System.Text.RegularExpressions.MatchCollection matchCollection2 = new System.Text.RegularExpressions.Regex("(?<=charset=\")(.*?)(?=\")", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input2);
												System.Text.RegularExpressions.MatchCollection matchCollection3 = new System.Text.RegularExpressions.Regex("(?<=charset=)(.*?)(?=\")", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Matches(input2);
												if (matchCollection2.Count <= 0)
												{
													if (matchCollection3.Count > 0)
													{
														string_0 = matchCollection3[0].Value.Trim();
													}
												}
												else
												{
													string_0 = matchCollection2[0].Value.Trim();
												}
											}
										}
									}
									else
									{
										string_0 = "ASCII";
									}
								}
								else
								{
									string_0 = "Unicode";
								}
							}
							else
							{
								string_0 = "BIG5";
							}
						}
						else
						{
							string_0 = "GB2312";
						}
					}
					else
					{
						string_0 = "UTF-8";
					}
				}
				else
				{
					string_0 = "GBK";
				}
				GClass38.smethod_2(ref string_0);
			}
		}
	}
}
